package com.zmm.myyimixian.ui.activity.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.zmm.myyimixian.MyApp;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.ui.activity.main.dingdan.DingDanFragment;
import com.zmm.myyimixian.ui.activity.main.fenlei.FeiLeiFragment;
import com.zmm.myyimixian.ui.activity.main.home.HomeFragment;
import com.zmm.myyimixian.ui.activity.main.me.MeFragment;
import com.zmm.myyimixian.ui.adapter.MyMainAdapter;
import com.zmm.myyimixian.util.UIManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.main_vp)
    ViewPager mainVp;
    @BindView(R.id.mian_home_rb)
    Button mianHomeRb;
    @BindView(R.id.mian_fenlei_rb)
    Button mianFenleiRb;
    @BindView(R.id.mian_car_iv)
    ImageView mianCarIv;
    @BindView(R.id.mian_dingd_rb)
    Button mianDingdRb;
    @BindView(R.id.mian_me_rb)
    Button mianMeRb;
    private Unbinder bind;
    protected void initView() {
        List<Fragment> list = new ArrayList<>();
        HomeFragment homeFragment = new HomeFragment();
        FeiLeiFragment feileiFragment = new FeiLeiFragment();
        DingDanFragment dingdFragment = new DingDanFragment();
        final MeFragment meFragment = new MeFragment();
        list.add(homeFragment);
        list.add(feileiFragment);
        list.add(dingdFragment);
        list.add(meFragment);
        MyMainAdapter adapter = new MyMainAdapter(getSupportFragmentManager());
        adapter.setData(list);
        mainVp.setAdapter(adapter);
        mainVp.setOffscreenPageLimit(3);
        //设置默认显示
        mainVp.setCurrentItem(0);
        //设置监听同步下方按钮
        mainVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        mianHomeRb.setBackgroundResource(R.mipmap.home_home_icon_p);
                        mianFenleiRb.setBackgroundResource(R.mipmap.ic_home_kind);
                        mianDingdRb.setBackgroundResource(R.mipmap.home_order_icon);
                        mianMeRb.setBackgroundResource(R.mipmap.home_account_icon);
                        break;
                    case 1:
                        mianHomeRb.setBackgroundResource(R.mipmap.home_home_icon);
                        mianFenleiRb.setBackgroundResource(R.mipmap.ic_home_kind_p);
                        mianDingdRb.setBackgroundResource(R.mipmap.home_order_icon);
                        mianMeRb.setBackgroundResource(R.mipmap.home_account_icon);
                        break;
                    case 2:
                        mianHomeRb.setBackgroundResource(R.mipmap.home_home_icon);
                        mianFenleiRb.setBackgroundResource(R.mipmap.ic_home_kind);
                        mianDingdRb.setBackgroundResource(R.mipmap.home_order_icon_p);
                        mianMeRb.setBackgroundResource(R.mipmap.home_account_icon);
                        break;
                    case 3:
                        mianHomeRb.setBackgroundResource(R.mipmap.home_home_icon);
                        mianFenleiRb.setBackgroundResource(R.mipmap.ic_home_kind);
                        mianDingdRb.setBackgroundResource(R.mipmap.home_order_icon);
                        mianMeRb.setBackgroundResource(R.mipmap.home_account_icon_p);
                        break;
                }
            }
        });
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
        setContentView(R.layout.activity_main);
        bind = ButterKnife.bind(this);
        //全屏显示
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,WindowManager.LayoutParams. FLAG_FULLSCREEN);
        initView();
        overridePendingTransition(R.anim.activity_open,R.anim.activity_cloes);
    }
    @OnClick({R.id.mian_home_rb, R.id.mian_fenlei_rb, R.id.mian_car_iv, R.id.mian_dingd_rb, R.id.mian_me_rb})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mian_home_rb:
                mainVp.setCurrentItem(0);
                break;
            case R.id.mian_fenlei_rb:
                mainVp.setCurrentItem(1);
                break;
            case R.id.mian_car_iv:
                if (MyApp.isLogin()) {
                    UIManager.startShoppingCartActivity(this);
                }else {
                    UIManager.startLoginActivity(this);
                }
                break;
            case R.id.mian_dingd_rb:
                mainVp.setCurrentItem(2);
                break;
            case R.id.mian_me_rb:
                mainVp.setCurrentItem(3);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bind != null) {
            bind.unbind();
        }
    }

    private int mCount = 0;
    private long fastTime;
    //处理返回键
    @Override
    public void onBackPressed() {
        if (mCount != 0 &&System.currentTimeMillis() - fastTime < 2000) {
            //                finishAffinity();//两次点击返回键小于两秒就退出程序，否则不退出
            finish();
        } else {
            fastTime = System.currentTimeMillis();
            //获取第一次点击返回键的系统时间
            Toast.makeText(MainActivity.this, "再按一次退出一米鲜", Toast.LENGTH_SHORT).show();
            mCount++;
        }
    }
}
